//表格配置
var dataTableconfig = {
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/auth_template/queryAuthTemplate",
		columns : [
				{
					data : "AUTH_ID",
					render : function(data, type, full) {
						return '<input name=\"items\" type="checkbox" id=\"check_'+data+'\" value="' + data + '" name="subBox" />';
					}
				}, {
					"data" : "AUTH_ID"
				},{
					"data" : "NAME"
				}, {
					"data" : "IS_USE"
				}],
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",

	};

//初始化表格
var table= $('#auth_table').DataTable(dataTableconfig);

$(function() {
	//全选
	$("#checkall").click(function() {
		$('[name=items]:checkbox').prop("checked", this.checked);
	});
	$('[name=items]:checkbox').click(
			function() {
				var $tmp = $('[name=items]:checkbox');
				$('#checkall').prop('checked',
					$tmp.length == $tmp.filter(':checked').length);
				});

	//输出值
	$("#send").click(function() {
		var str = "你选中的是：\r\n";
		$('[name=items]:checkbox:checked').each(function() {
			str += $(this).val() + "\r\n";
		})
		alert(str);
	});
});

var app = angular.module('authApp', []);
var initController = app.controller(
	"authController",
	function($scope, $http) {
		$('#myModal').on('hide.bs.modal', function () {
			document.getElementById("addForm").reset(); 
		});
		$scope.add = function(){
			var form = document.getElementById("addForm");
			var authTemplateName = form.authTemplateName.value;
			var isUse = form.is_use.value;
			$http({
				url : '/auth_template/add',
				params:{authTemplateName:authTemplateName, isUse:isUse},
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				if(data=="1"){
					$('#addSuccessAlert').fadeIn(2000);
					$('#addSuccessAlert').fadeOut(2000);
					table.draw();
				}else{
					$('#addFailedAlert').fadeIn(2000);
					$('#addFailedAlert').fadeOut(2000);
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
				$('#addFailedAlert').fadeIn(2000);
				$('#addFailedAlert').fadeOut(2000);
			});
		}
		
		$scope.del_init=function(){
			var menuIds = "";
			$('[name=items]:checkbox:checked').each(function() {
				menuIds += $(this).val() + ";";
			});
			if(menuIds==""){
				$('#SelAlert').html("<strong>请选择记录进行删除！</strong>");
				$('#alertModal').modal("toggle");
			}else{
				$("#myModal3").modal("toggle");
			}
			
		};
		
		//删除---begin
		$scope.del = function(){
			var authIds = "";
			$('[name=items]:checkbox:checked').each(function() {
				authIds += $(this).val() + ";";
			});
			if(authIds==""){
				alert("请选择要删除的记录！");
			}else{
				$http({
					url : '/auth_template/delete',
					params:{authIds:authIds},
					method : 'GET'
				}).success(function(data, header, config, status) {
					// 响应成功
					if(data==true){
						$('#promptContent').hide();
						$('#del').hide();
						$('#deleteSuccessAlert').fadeIn(2000);
						$('#myModal3').on('hide.bs.modal', function () {
							table.draw();
							$('#del').show();
							$('#deleteSuccessAlert').hide();
							$('#promptContent').show();
						});
					}else{
						$('#promptContent').hide();
						$('#del').hide();
						$('#deleteFailedAlert').fadeIn(2000);
						$('#myModal3').on('hide.bs.modal', function () {
							table.draw();
							$('#del').show();
							$('#deleteFailedAlert').hide();
							$('#promptContent').show();
						});
					}
				}).error(function(data, header, config, status) {
					// 处理响应失败
					$('#promptContent').hide();
					$('#del').hide();
					$('#deleteFailedAlert').fadeIn(2000);
					$('#myModal3').on('hide.bs.modal', function () {
						$('#del').show();
						$('#deleteFailedAlert').hide();
						$('#promptContent').show();
					});
				});
			}
			
		};
		//删除---end
		
		//更新初始化----begin
		$scope.update_init =function(){
			var authIds = "";
			$('[name=items]:checkbox:checked').each(function() {
				authIds += $(this).val() + ";";
			});
			if(authIds==""){
				$('#SelAlert').html("<strong>请选择要修改的记录！</strong>");
				$('#alertModal').modal("toggle");
			}else if(authIds.split(";").length>2){
				$('#SelAlert').html("<strong>修改的记录数不能大于1.</strong>");
				$('#alertModal').modal("toggle");
			}else{
				var table = document.getElementById("auth_table");
				 $("table :checkbox").each(function(key,value){
			            if($(value).prop('checked')){
			            	$scope.auth_id_update = table.rows[key].cells[1].innerHTML;
			            	$scope.authTemplateName_update = table.rows[key].cells[2].innerHTML;
			            	$scope.is_use_update = table.rows[key].cells[3].innerHTML;
			            }
				 });
				 $("#myModal2").modal("toggle");
			}
		}
		//更新初始化----end
		
		//更新事件-----begin
		$scope.update = function(){
			var form = document.getElementById("updateForm");
			var auth_id = form.auth_id_update.value;
			var auth_name = form.authTemplateName_update.value;
			var is_use = form.is_use_update.value;
			$http({
				url : '/auth_template/edit',
				params:{auth_id:auth_id, auth_name:auth_name, is_use:is_use},
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				if(data=="1"){
					$('#updateSuccessAlert').fadeIn(2000);
					$('#updateSuccessAlert').fadeOut(2000);
					table.draw();
				}else{
					$('#updateFailedAlert').fadeIn(2000);
					$('#updateFailedAlert').fadeOut(2000);
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
				$('#updateFailedAlert').fadeIn(2000);
				$('#updateFailedAlert').fadeOut(2000);
			});
		}
		//更新事件-----end
		
		//查询时间-----begin
		$scope.query = function () {
			var url = '/auth_template/queryAuthTemplate?test=test';
			var form =document.getElementById("queryForm");  // $('#queryForm');
			if(form.auth_template_name_query.value!=0){
				url = url + '&auth_template_name=' + form.auth_template_name_query.value;
			}
			if(form.is_use_query.value!=0){
				url = url + '&is_use=' + form.is_use_query.value;
			}
			$('#auth_table').dataTable().api().destroy();
			dataTableconfig.ajax=url;
			DataTables=$('#auth_table').dataTable(dataTableconfig);
		};
		//查询事件-----end
	}
);